<style scoped>
.dashboard-editor-container {
  padding: 18px 22px 22px 22px;
  background-color: rgb(240, 242, 245);
  margin-bottom: 80px;
}

.input-with-select .el-input-group__prepend {
  background-color: #fff;
}

.crud-opts .crud-opts-right {
  margin-left: auto;
}

</style>

<template>
  <div class="dashboard-container">
    <div class="dashboard-editor-container" v-if="isShow == true">
      <el-card class="box-card" v-if="crud.optPageShow.listPage">
        <div slot="header" class="clearfix">
          <span>任务定义列表</span>
        </div>
        <!--工具栏-->
        <div class="head-container">
          <crudOperation :permission="permission"/>
        </div>
        <!--表格渲染-->
        <el-table ref="table" v-loading="crud.loading" v-if="!this.addDialog" :data="crud.data" style="width: 100%;"
                  @select="crud.selectChange"
                  @select-all="crud.selectAllChange" @selection-change="crud.selectionChangeHandler"
                  @filter-change="crud.handleFilterChange"
                  @row-dblclick="editClick"
                  @cell-mouse-enter="showTooltip"
                  @cell-mouse-leave="hideTooltip"
                  :header-cell-style="{ background: 'rgba(52, 125, 225, 0.1)', color: '#404248' }" border stripe>
          <el-table-column v-if="false" prop="pkId" label="id"/>
          <el-table-column v-if="columns.visible('appCode')" :show-overflow-tooltip="true" width="150"
                           prop="appCode" label="应用编码"/>
          <el-table-column v-if="columns.visible('taskCode')" :show-overflow-tooltip="true" prop="taskCode"
                           label="任务编码"/>
          <el-table-column v-if="columns.visible('taskName')" :show-overflow-tooltip="true" prop="taskName"
                           label="任务名称"/>
          <el-table-column v-if="columns.visible('cronExpress')" :show-overflow-tooltip="true" min-width="100"
                           prop="cronExpress" label="CRON表达式"/>
          <el-table-column v-if="columns.visible('shellPath')" :show-overflow-tooltip="true" prop="shellPath"
                           label="shell文件"/>
          <el-table-column v-if="columns.visible('extParam')" :show-overflow-tooltip="true" prop="extParam"
                           label="外部参数"/>
          <el-table-column v-if="columns.visible('taskStatus')" prop="taskStatus" label="状态 " min-width="100"
                           :filters="taskDefStatusDict" key="taskStatus"
                           :filter-method="filterTaskStatus" filter-placement="bottom-end" :filter-multiple="false"
                           column-key="taskStatus">
            <template slot-scope="scope">
              <el-tag size="medium" style="width:60px;" :type="taskStatusFormatterType(scope.row.taskStatus)">
                {{ taskStatusFormatter(scope.row.taskStatus) }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column v-if="columns.visible('upTs')" :show-overflow-tooltip="true" min-width="160" prop="upTs"
                           label="最后更新时间">
            <template slot-scope="scope">
              <!-- 悬浮提示 -->
              <el-tooltip
                placement="bottom-end"
                v-model="scope.row.showTooltip"
                :open-delay="500"
                effect="light"
                :disabled="!scope.row.showTooltip"
              >
                <!-- 注意 显示单元格浮框时 v-model 和 disabled 属性要一起使用才有效果 -->
                <div slot="content">双击当前行点开详情</div>
                <div>
                  <i class="el-icon-time"></i>
                  <span style="margin-left: 10px">{{ scope.row.upTs }}</span>
                </div>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <div class="dashboard-editor-container">
      <sySTaskInfo v-if="showDetail" :taskCode="taskCode" ref="tsInfo" :goBack="goBack"/>
    </div>
    <el-dialog append-to-body :close-on-click-modal="false" :visible.sync="crud.backUp" title="定时任务实例备份" width="680px">
      <el-form ref="form" :inline="true" :model="form" size="small" label-width="160px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="备份日期">
              <el-date-picker style="margin-left: 50px" v-model="backUpDate" type="date" value-format="yyyy-MM-dd"
                              placeholder="选择日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button icon="el-icon-document-delete" @click="backUpCancel()">取消</el-button>
        <el-button icon="el-icon-document-add" type="primary" @click="backUpSubmit()">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import cronTaskDefApi from '@/api/cronTask/cronTaskDef'
import cron from '@/components/cron/cron'
import CRUD, {presenter, header, form, crud} from '@crud/crud'
import crudOperation from '@crud/extend/components/CRUD.OnlyLookOperation'
import sySTaskInfo from '../components'
import backUp from '@/api/backUp/backUp'

const queryList = [{
  'cloumValue': 'appCode',
  'cloumName': '应用编码'
}, {
  'cloumValue': 'taskCodeFuzzy',
  'cloumName': '任务编码'
}, {
  'cloumValue': 'taskName',
  'cloumName': '任务名称'
}];
const defaultCrud = CRUD({
  title: '任务定义',
  queryList: queryList,
  url: '/api/cronTaskDef',
  sort: 'sort,asc',
  crudMethod: {...cronTaskDefApi}
})
const defaultForm = {}

const tableColumnOptions = [];
export default {
  name: 'cronDef',
  components: {
    cron,
    crudOperation,
    sySTaskInfo
  },
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  dicts: ['task_def_status'],
  filters: {
    formatTime: function (value) {
      if (value != '') {
        return value.slice(0, 19).replace('T', ' ')
      }
    }
  },
  data() {
    return {
      backUpDate: "",
      isShow: true,
      active: 0,
      execType: '',
      permission: {
        nodeQuery: ['admin', 'cronDef:nodeQuery']
      },
      allColumnsSelected: true,
      allColumnsSelectedIndeterminate: false,
      checkAll: true,
      tableColumns: tableColumnOptions,
      isIndeterminate: true,
      qryParam: '',
      qryCloum: '',
      showQryParamFlag: false,
      addDialog: false,
      rules: {},
      zones: ['linux', 'windows', 'ios'],
      uuidA: '',
      uploadTemplateDialog: false,
      fileList: [],
      uploadLoading: false,
      // acceptFileType:'.xls',
      downLoadLoading: '',
      showDetail: false,
      taskDefDict: [],
      taskDefStatusDict: []
    }
  },
  computed: {},
  created() {
    this.active = 0;
    this.taskDefDict = this.dict.task_def_status;
    this.initData()
  },
  watch: {
    taskDefDict() {
      this.$nextTick(() => {
        this.taskDefDict.forEach(item => {
          var obj = {
            text: item.label,
            value: item.value
          }
          this.taskDefStatusDict.push(obj)
        })
      })
    },
  },
  methods: {
    taskStatusFormatterType(val) {
      const type = ['info', 'success', 'primary']
      return type[val]
    },
    taskStatusFormatter(val) {
      return this.dict.label.task_def_status[val]
    },
    backUpCancel() {
      this.crud.backUp = false
      this.backUpDate = ""
    },
    backUpSubmit() {
      var data = {
        "dateTs": this.backUpDate,
        "bitchType": "cycle"
      }
      backUp.backUpData(data)
        .then(() => {
          this.$notify({
            title: '成功',
            message: '数据备份成功',
            type: 'success'
          });
          this.backUpDate = ""
          this.crud.backUp = false
          this.crud.query = {}
          this.crud.params = {}
          this.crud.toQuery()
        })
        .catch(() => {
          this.$notify.error({
            title: '错误',
            message: '数据备份异常'
          });
        });
    },
    //进入时显示提示框
    showTooltip(row) {
      //需要显示浮框的条件
      //if (row.filterFlag) {
      row.showTooltip = true;
      //}
    },
    //退出时隐藏提示框
    hideTooltip(row) {
      row.showTooltip = false;
    },
    editClick(row, column, event) {
      this.showDetail = true;
      this.isShow = false;
      this.taskCode = row.taskCode;
      /*this.$refs.tsInfo.getTaskInfByTaskCode(row.taskCode)*/
    },
    formatterBatchTag(val) {
      return val;
    },
    filterTaskStatus(value, row) {
      return row.taskStatus === value;
    },
    formatterOrderStatusType(val) {
      const type = ['', 'success', 'warning', 'success', 'danger', 'info', 'danger']
      return type[val]
    },
    formatterOrderStatus(val) {
      return this.dict.label.task_def_status[val]
    },
    filterExecType(value, row) {
      return row.execType === value;
    },
    execTypeFormatter(row, column) {
      return this.dict.label.exec_type[row.execType]
    },
    handleCheckAllChange(val) {
      if (val === false) {
        this.$message({
          message: '请至少选择一列',
          type: 'warning'
        });
        this.allColumnsSelected = true
        return
      }
      for (const key in this.crud.props.tableColumns) {
        this.crud.props.tableColumns[key].visible = val
      }
      this.allColumnsSelected = val
      this.allColumnsSelectedIndeterminate = false
    },
    handleCheckedTableColumnsChange(value) {
      let totalCount = 0
      let selectedCount = 0
      for (const key in this.crud.props.tableColumns) {
        ++totalCount
        selectedCount += this.crud.props.tableColumns[key].visible ? 1 : 0
      }
      if (selectedCount === 0) {
        this.crud.notify('请至少选择一列', CRUD.NOTIFICATION_TYPE.WARNING)
        this.$nextTick(function () {
          item.visible = true
        })
        return
      }
      this.allColumnsSelected = selectedCount === totalCount
      this.allColumnsSelectedIndeterminate = selectedCount !== totalCount && selectedCount !== 0
    },
    handleCheckAllChange(val) {
      this.checkedTableColumns = val ? tableColumnOptions : [];
      this.isIndeterminate = false;
    },
    handleCheckedTableColumnsChange(value) {
      let checkedCount = value.length;
      this.checkAll = checkedCount === this.tableColumns.length;
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.tableColumns.length;
    },
    showQryParams() {
      this.showQryParamFlag = true
    },
    hideQryParams() {
      this.showQryParamFlag = false
    },
    initData() {

    },
    add() {
      this.addDialog = true
    },
    update() {
    },
    del() {
    },
    backFun() {
      this.addDialog = false;
    },
    goBack() {
      this.showDetail = false;
      this.isShow = true;
    }
  }
}
</script>
